﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class PollEdit : System.Web.UI.Page
{

    #region VARIABLES

    List<PollAnswer> _CurrentAnswers = null;

    #endregion VARIABLES

    #region PROPERTIES

    List<PollAnswer> CurrentAnswers
    {
        get
        {
            if (_CurrentAnswers == null)
            {
                if (Session["CurrentAnswers"] != null)
                    _CurrentAnswers = (List<PollAnswer>)Session["CurrentAnswers"];
                else
                    _CurrentAnswers = new List<PollAnswer>();
            }

            return _CurrentAnswers;
        }
        set
        {
            _CurrentAnswers = value;
            Session["CurrentAnswers"] = value;
        }
    }

    int NewAnswerID
    {
        get
        {
            if (ViewState["NewAnswerID"] == null)
                return 0;
            else
                return (int)ViewState["NewAnswerID"];
        }

        set
        {
            ViewState["NewAnswerID"] = value;
        }
    }

    int CurrentPollID
    {
        get
        {
            if (ddlPolls.SelectedValue != string.Empty)
                return int.Parse(ddlPolls.SelectedValue);
            else
                return -1;
        }
    }

    #endregion PROPERTIES

    #region METHODS

    protected void RefreshPollsList(string SelectValue)
    {
        ddlPolls.Items.Clear();

        ddlPolls.Items.Add(new ListItem(Resources.Language.cmsAddNew, ""));
        ddlPolls.DataSource = ShopBLL.ReportsManager.Instance.GetPolls(int.Parse(ddlLanguage.SelectedValue));
        ddlPolls.DataBind();

        ddlPolls.SelectedValue = SelectValue;

        if (ddlPolls.SelectedValue != string.Empty)
        {
            btnDeletePollTop.Enabled = true;
            btnDeletePollBottom.Enabled = true;
        }
    }

    protected void ClearPollData()
    {
        tbQuestion.Text = string.Empty;
        tbName.Text = string.Empty;
        cbIsActive.Checked = false;
        ddlCategory.SelectedIndex = 0;
        lDateCreated.Text = string.Empty;
        CurrentAnswers = null;
        btnDeletePollTop.Enabled = false;
        btnDeletePollBottom.Enabled = false;
        rptAnswerPoll.DataSource = null;
        rptAnswerPoll.DataBind();
    }

    protected void RefreshPollData()
    {
        Poll CurrentPoll = null;
        if (CurrentPollID > 0)
            CurrentPoll = ReportsManager.Instance.GetPoll(int.Parse(ddlPolls.SelectedValue), null, int.Parse(ddlLanguage.SelectedValue));

        if (CurrentPoll != null)
        {
            tbQuestion.Text = CurrentPoll.Question;
            tbName.Text = CurrentPoll.Name;
            cbIsActive.Checked = CurrentPoll.IsActive;
            lDateCreated.Text = CurrentPoll.DateCreated.ToShortDateString();
            CurrentAnswers = CurrentPoll.Answers;
            btnDeletePollTop.Enabled = true;
            btnDeletePollBottom.Enabled = true;
            BindAnswers();
        }
        else
        {
            ClearPollData();
        }
    }

    protected void BindAnswers()
    {
        rptAnswerPoll.DataSource = CurrentAnswers;
        rptAnswerPoll.DataBind();
    }

    protected bool _Validate()
    {
        bool valid = true;

        if (!rfvName.IsValid)
        {
            CtrlShowInfo.AppendError(Resources.Language.FillInPollName);
            valid = false;
        }

        if (!rfvQuestion.IsValid)
        {
            CtrlShowInfo.AppendError(Resources.Language.FillInPollQuestion);
            valid = false;
        }

        if (CurrentAnswers.Count < 2)
        {
            CtrlShowInfo.AppendError(Resources.Language.TwoAnswersRequired);
            valid = false;
        }

        return valid;
    }

    #endregion METHODS

    #region EVENTS

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlPolls.SelectedValue != string.Empty)
            RefreshPollData();
    }

    protected void ddlPolls_SelectedIndexChanged(object sender, EventArgs e)
    {
        RefreshPollData();
    }

    protected void Poll_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "New":
                ddlPolls.SelectedIndex = 0;
                RefreshPollData();
                break;
            case "Save":
                if (_Validate())
                {
                    Poll PollToSave = new Poll();

                    if (ddlCategory.SelectedValue != string.Empty)
                        PollToSave.CategoryID = int.Parse(ddlCategory.SelectedValue);

                    if (ddlPolls.SelectedValue != string.Empty)
                        PollToSave.IDPoll = int.Parse(ddlPolls.SelectedValue);

                    PollToSave.IsActive = cbIsActive.Checked;
                    PollToSave.Name = tbName.Text;
                    PollToSave.Question = tbQuestion.Text;

                    int? NewID = ReportsManager.Instance.SavePoll(PollToSave, int.Parse(ddlLanguage.SelectedValue));

                    if (NewID != null)
                    {
                        ReportsManager.Instance.SavePollAnswers(NewID.Value, CurrentAnswers, int.Parse(ddlLanguage.SelectedValue));
                        CtrlShowInfo.AppendInfo(Resources.Language.PollSavedSuccesfully);
                        RefreshPollsList(NewID.ToString());
                    }
                    else
                        CtrlShowInfo.AppendError(Resources.Language.ErrorSavingPoll);
                }
                break;
            case "Delete":
                ReportsManager.Instance.DeletePoll(int.Parse(ddlPolls.SelectedValue));
                RefreshPollsList(string.Empty);
                RefreshPollData();
                break;
            case "Cancel":
                RefreshPollData();
                break;
        }
    }

    protected void Answer_Command(object sender, CommandEventArgs e)
    {
        int EditedPollAnswerID = 0;

        switch (e.CommandName)
        {
            case "Add":
                if (tbAnswer.Text != string.Empty && !CurrentAnswers.Exists(delegate(PollAnswer pa) { return pa.Answer == tbAnswer.Text; }))
                {
                    CurrentAnswers.Add(new PollAnswer(-(++NewAnswerID), CurrentPollID, tbAnswer.Text, 0));
                    tbAnswer.Text = string.Empty;
                }
                break;
            case "Save":
                if (!CurrentAnswers.Exists(delegate(PollAnswer pa) { return pa.Answer == tbAnswer.Text; }))
                    if (hfEditedPollAnswerID.Value != string.Empty)
                    {
                        EditedPollAnswerID = int.Parse(hfEditedPollAnswerID.Value);
                        CurrentAnswers.Find(delegate(PollAnswer pa) { return pa.IDPollAnswer == EditedPollAnswerID; }).Answer = tbAnswer.Text;
                        hfEditedPollAnswerID.Value = string.Empty;
                        tbAnswer.Text = string.Empty;
                    }
                break;
            case "Edit":
                if (e.CommandArgument != null)
                {
                    EditedPollAnswerID = Convert.ToInt32(e.CommandArgument);
                    tbAnswer.Text = CurrentAnswers.Find(delegate(PollAnswer pa) { return pa.IDPollAnswer == EditedPollAnswerID; }).Answer;
                    hfEditedPollAnswerID.Value = EditedPollAnswerID.ToString();
                }
                break;
            case "Delete":
                if (e.CommandArgument != null)
                {
                    EditedPollAnswerID = Convert.ToInt32(e.CommandArgument);
                    CurrentAnswers.RemoveAll(delegate(PollAnswer pa) { return pa.IDPollAnswer == EditedPollAnswerID; });
                }
                break;
            case "Cancel":
                hfEditedPollAnswerID.Value = string.Empty;
                tbAnswer.Text = string.Empty;
                break;
        }
        Session["CurrentAnswers"] = CurrentAnswers;
        BindAnswers();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlLanguage.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
            ddlLanguage.DataBind();

            ddlLanguage.SelectedValue = CommonTools.CurrentLanguageID.ToString();

            Category categoryTree = ProductsManager.Instance.GetChildernTreeCategories(-1, true, ShopBLL.CommonTools.CurrentLanguageID);
            CommonTools.FillCategoryTree(categoryTree, ddlCategory, true);
            ddlCategory.Items.Insert(0, new ListItem("", ""));

            RefreshPollsList(string.Empty);
            ClearPollData();
        }
    }

    #endregion PAGE EVENTS
}
